using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using PIT.Labs.SharePoint.Logger;
using Microsoft.SharePoint.Administration;
using PIT.Labs.SharePoint.Extensions.Helper;

namespace PIT.Labs.ProjectRepository.WebParts.ListRandomizerWebPart.Features.RandomizerTimerFeature
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("8b3dcaf0-3e6d-428a-b25b-bf1bb6a036c3")]
    public class RandomizerTimerFeatureEventReceiver : SPFeatureReceiver
    {
        /// <summary>
        /// The unique name of the randomizer timer.
        /// </summary>
        public const String RANDOMIZER_TIMER_NAME = "List Randomizer Timer";

        /// <summary>
        /// Handles the event that is raised after a Feature is activated. In this case the List Randomizer Timer is created.
        /// </summary>
        /// <param name="properties">The properties of the event.</param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
                RemoveRandomizerTimer(webApp);

                RandomizerTimerJob workitemTimerJob = new RandomizerTimerJob(RANDOMIZER_TIMER_NAME, webApp);
                SPMinuteSchedule webConfigSchedule = new SPMinuteSchedule();
                webConfigSchedule.Interval = 1;
                webConfigSchedule.BeginSecond = 0;
                webConfigSchedule.EndSecond = 59;
                workitemTimerJob.Schedule = webConfigSchedule;
                workitemTimerJob.Update();
                workitemTimerJob.RunNow();
            }
            catch (Exception ex)
            {
                SPLogger logger = new SPLogger();
                logger.Log(NLog.LogLevel.Error, SPUtilityHelper.GetLocalizedString("ERR006", ListRandomizerWebPart.ListRandomizerWebPart.RESOURCES_FILE), ex, true);
            }
        }

        private void RemoveRandomizerTimer(SPWebApplication webApp)
        {
            foreach (SPJobDefinition job in webApp.JobDefinitions)
            {
                if (job.Name == RANDOMIZER_TIMER_NAME)
                {
                    job.Delete();
                }
            }
        }


        /// <summary>
        /// Handles the event that is raised when a Feature is deactivated. In this case: Removes all Randomizer Timers.
        /// </summary>
        /// <param name="properties">he properties of the event.</param>
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
                RemoveRandomizerTimer(webApp);
            }
            catch (Exception ex)
            {
                SPLogger logger = new SPLogger();
                logger.Log(NLog.LogLevel.Error, SPUtilityHelper.GetLocalizedString("ERR007", ListRandomizerWebPart.ListRandomizerWebPart.RESOURCES_FILE), ex, true);
            }
        }
    }
}
